// src/interceptor/transform.interceptor.ts
import { CallHandler, ExecutionContext, Injectable, NestInterceptor } from '@nestjs/common';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { Logger } from 'src/utils/log4js';
import { getLogStr } from '../common/utils';

@Injectable()
export class TransformInterceptor implements NestInterceptor {
    intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
        const req = context.getArgByIndex(1).req;
        return next.handle().pipe(
            map(data => {
        const logFormat =`Request original url: ${req.originalUrl}
        Method: ${req.method}
        IP: ${req.ip}
        Response data:\n ${JSON.stringify(data)}`;
                Logger.info(getLogStr(logFormat));
                Logger.access(getLogStr(logFormat));
                return data;
            })
        )
    }
}
